package cn.vendai.com.config;

import cn.vendai.com.properties.PineconeProperties;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.pinecone.PineconeEmbeddingStore;
import dev.langchain4j.store.embedding.pinecone.PineconeServerlessIndexConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EmbeddingStoreConfig {

    private final PineconeProperties pineconeProperties;
    private final EmbeddingModel embeddingModel;

    public EmbeddingStoreConfig(PineconeProperties pineconeProperties,
                                EmbeddingModel embeddingModel) {
        this.pineconeProperties = pineconeProperties;
        this.embeddingModel = embeddingModel;
    }

    @Bean
    public EmbeddingStore<TextSegment> embeddingStore() {
        return PineconeEmbeddingStore.builder()
                .apiKey(pineconeProperties.getApiKey())
                .index(pineconeProperties.getIndex())
                .nameSpace(pineconeProperties.getNamespace())
                .createIndex(PineconeServerlessIndexConfig.builder()
                        .cloud(pineconeProperties.getCloud())
                        .region(pineconeProperties.getRegion())
                        .dimension(embeddingModel.dimension())
                        .build())
                .build();
    }
}
